package com.etc.service.impl;

import com.etc.dao.StudentMapper;
import com.etc.dao.User_attendanceMapper;
import com.etc.dao.User_classMapper;
import com.etc.model.Student;
import com.etc.model.User_attendance;
import com.etc.model.User_class;
import com.etc.service.IFileToExcelService;
import com.etc.service.IFileUploadService;
import com.etc.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class IFileHanderUserAttendanceImpl {
    @Autowired
    private IFileToExcelService iFileToExcelService;
    @Autowired
    private IFileUploadService iFileUploadService;
    @Autowired
    private User_classMapper classInfo;
    @Autowired
    private StudentMapper studentInfo;
    @Autowired
    private User_attendanceMapper user_attendanceInfo;
    public List<StudentVo> IFileHanderUserAttendance(List<User_class> studentList,String classnodeUpload){
        List<StudentVo> studentVos = new ArrayList<>();
        for(User_class studata:studentList){
            //判断班级是否有人
            User_class FirstData = classInfo.findFirstByClassnodeAndUsertype(classnodeUpload, "学生");
            if (studata.getUsername() != null) {
                classInfo.insert(studata);
                //班级数量加一
                List<User_class> list = classInfo.findAllByClassnode(classnodeUpload);
                for (User_class classdata : list) {
                    //班级人数加1
                    classdata.setClassnumber(classdata.getClassnumber() + 1);
                    classInfo.updateByPrimaryKey(classdata);
                }
                //默认考勤日期
                Student student = studentInfo.findFirstByUsername(studata.getUsername());
                if (FirstData == null) {
                    for (int i = 0; i < 4; i++) {
                        //当前时间
                        String TimeNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
                        User_attendance user_data = new User_attendance();
                        user_data.setAttendance(TimeNow);
                        user_data.setClassnode(classnodeUpload);
                        user_data.setName(studata.getName());
                        user_data.setStatus("1");
                        user_data.setStudentnumber(student.getStudentnumber());
                        user_data.setUsername(studata.getUsername());
                        user_attendanceInfo.insert(user_data);
                    }
                } else {
                    List<User_attendance> list1 = user_attendanceInfo.findAllByUsernameAndClassnode(FirstData.getUsername(),classnodeUpload);
                    for (User_attendance data3 : list1) {
                        User_attendance user_data = new User_attendance();
                        user_data.setAttendance(data3.getAttendance());
                        user_data.setClassnode(data3.getClassnode());
                        user_data.setName(studata.getName());
                        user_data.setStatus("1");
                        user_data.setStudentnumber(student.getStudentnumber());
                        user_data.setUsername(studata.getUsername());
                        user_attendanceInfo.insert(user_data);
                    }
                }
                studentVos.add(new StudentVo("已注册", studata));
            }
            else {
                studentVos.add(new StudentVo("未注册",studata));
            }



        }
       return  studentVos;
    }
}
